﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using WPFMediaKit.DirectShow.Controls;

namespace Wpf3dCollaborative
{
    /// <summary>
    /// Logique d'interaction pour MenuCam.xaml
    /// </summary>
    public partial class MenuCam : Canvas
    {

        public Cam CurrentCam = null; // caméra courante
        public MenuCam()
        {
            InitializeComponent();
            Visibility = Visibility.Collapsed;

           ComboBoxItem tmp= new ComboBoxItem();
           tmp.Content = "Aucune";
           

            videoCapDevices.Items.Add(tmp);
            for (int i = 0; i < MultimediaUtil.VideoInputDevices.Length; ++i)
            { //MultimediaUtil.VideoInputDevices
                videoCapDevices.Items.Add(MultimediaUtil.VideoInputDevices[i]);
            }
            videoCapDevices.SelectedIndex = 0;
        }

        public void SetOwnerCam(Cam OwnerCam)
        {
            CurrentCam = null;
            CurrentCam = OwnerCam;
        }

        private void checkcam_Checked(object sender, RoutedEventArgs e)
        {
            videoCapDevices.Visibility = Visibility.Collapsed;
            campIp.Visibility = Visibility.Visible;

        }

        private void checkcam_Unchecked(object sender, RoutedEventArgs e)
        {
            videoCapDevices.Visibility = Visibility.Visible;
            campIp.Visibility = Visibility.Collapsed;
        }

        private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }

        private void BtnInverser_Click(object sender, RoutedEventArgs e)
        {
            CurrentCam.Inverser();
        }

        private void BtnPlay_Click(object sender, RoutedEventArgs e)
        {

            if (!CurrentCam.IsPlaying)
            {
                CurrentCam.Play();
                BtnPlay.Content = "||";
            }
            else
            {
                CurrentCam.Pause();            
               
                BtnPlay.Content = "►";
            }

        }

        private void BtnStop_Click(object sender, RoutedEventArgs e)
        {
           
                CurrentCam.Stop();            
        }

   

        private void Mode16_Click(object sender, RoutedEventArgs e)
        {
            CurrentCam.SetWideScreenCam();
          
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            videoCapDevices.Visibility = Visibility.Collapsed;
            campIp.Visibility = Visibility.Visible;
        }

        private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            videoCapDevices.Visibility = Visibility.Visible;
            btnParcourir.Visibility = Visibility.Collapsed;
                    
            campIp.Visibility = Visibility.Collapsed;
        }

        private void videoCapDevices_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {

                CurrentCam.Source = ((DirectShowLib.DsDevice)videoCapDevices.SelectedValue).Name;
                CurrentCam.TypeCamera = Cam.TypeCam.WEBCAM;
            }
            catch ( Exception ex)
            {
               Console.WriteLine("[Ex Normale]:" +ex.Message);
            }
        }

        private void campIp_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (CurrentCam != null)
            {
                CurrentCam.Source = campIp.Text;
            }
        }

        private void campIp_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (checkVideo.IsChecked==true)
                {
                    CurrentCam.TypeCamera = Cam.TypeCam.VIDEO;
                }
                else
                {
                    CurrentCam.TypeCamera = Cam.TypeCam.IPCAM;

                    Console.WriteLine("Ipcam");
                }
            }
        }

        private void btnParcourir_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fd= new OpenFileDialog();
            fd.Filter = "Fichiers Vidéo (*.mpg, *.avi , *.divx,  *.mkv, *.vob,  *.wmv,  *.mpeg)|*.mpg;*.avi;*.divx;*.mkv;*.vob;*.wmv;*.mpeg|All files (*.*)|*.*";
          
            if (fd.ShowDialog() == true)
            {

                campIp.Text = fd.FileName;
                CurrentCam.Source = fd.FileName;
                CurrentCam.TypeCamera = Cam.TypeCam.VIDEO;


            }

          
        }

        private void checkVideo_Checked(object sender, RoutedEventArgs e)
        {
            videoCapDevices.Visibility = Visibility.Collapsed;
            campIp.Visibility = Visibility.Visible;
            campIp.Text = "Chemin de la video";
            campIp.ToolTip = "Cliquez sur parcourir pour choisir votre vidéo";
            TxtCam.Text = "Vidéo : ";
            btnParcourir.Visibility = Visibility.Visible;

        }

        private void checkVideo_Unchecked(object sender, RoutedEventArgs e)
        {
            btnParcourir.Visibility = Visibility.Collapsed;
            campIp.ToolTip = "Entrez l'url de votre caméra puis appuyez sur Entrer \nExemple : axrtsp://172.16.4.20:554/mpeg4/media.amp \nAttention : Le décodeur approprié doit être installé sur votre machine et référencé ";
            TxtCam.Text = "Caméra : ";
            campIp.Text = "Url de la cam Ip";

        }

        private void sliderVolume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            
            CurrentCam.SetVolume(Convert.ToDouble(sliderVolume.Value));
        }

     
      
    }
}
